﻿using System;
using System.Configuration;
using System.IO;
using System.Web.UI;

namespace AssignmentSWEN3.App_Code
{
    public class ConnectionClass
    {
        public ConnectionClass()
        {
		
        }

    public static string GetNew(Page page)
    {
        string today = DateTime.Today.ToString("yyyy-MM-dd");
        string guid = Guid.NewGuid().ToString();
        string dir = page.Server.MapPath("~/App_Data/session/" + today + "/");
        string master = page.Server.MapPath("~/App_Data/Database.mdf");
        string path =  dir + guid;

        Directory.CreateDirectory(dir);
        File.Copy(master, path);

        return String.Format("Data Source={0};New=true;", path);
    }

    public static string Get(Page page)
    {
        bool local = true;
        if (local)
        {
            return ConfigurationManager.ConnectionStrings["db"].ConnectionString;
        }

        if (page.Session["cs"] as string == null)
        {
            page.Session["cs"] = GetNew(page);
        }

        return (string)page.Session["cs"];

    }
    }
}